package cn.matezk.lianlian.framework;

import cn.matezk.lianlian.beans.PaymentProperty;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties(prefix = "wechat-payment")
public class PaymentConfiguration {
    private List<PaymentProperty> property;

    public NotificationConfig buildNotificationConfig(String merchantId) {
        for (PaymentProperty param : property) {
            if (param.getMerchantId().equals(merchantId)) {
                return new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        .privateKeyFromPath(param.getPrivateKeyPath())
                        .merchantSerialNumber(param.getMerchantSerialNumber())
                        .apiV3Key(param.getApiV3Key())
                        .build();
            }
        }
        throw new IllegalArgumentException(String.format("未配置商户信息: %s", merchantId));
    }

    public List<PaymentProperty> getProperty() {
        return property;
    }

    public void setProperty(List<PaymentProperty> property) {
        this.property = property;
    }
}
